/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.gui.studenci;

import db.models.Student;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import org.openide.awt.NotificationDisplayer;
import org.openide.windows.WindowManager;

/**
 *
 * @author Ja
 */
public class DodajEdytujStudentaPanel extends javax.swing.JPanel {

    /**
     * Creates new form DodajStudenta
     */
    public DodajEdytujStudentaPanel() {
        initComponents();
    }

    public DodajEdytujStudentaPanel(String imie, String nazwisko, String email, Integer indeksNr) {
        initComponents();
        imieField.setText(imie);
        nazwiskoField.setText(nazwisko);
        emailField.setText(email);
        indeksField.setText(indeksNr.toString());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        imieField = new javax.swing.JTextField();
        nazwiskoField = new javax.swing.JTextField();
        emailField = new javax.swing.JTextField();
        indeksField = new javax.swing.JTextField();

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(DodajEdytujStudentaPanel.class, "DodajEdytujStudentaPanel.jLabel1.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(DodajEdytujStudentaPanel.class, "DodajEdytujStudentaPanel.jLabel2.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(DodajEdytujStudentaPanel.class, "DodajEdytujStudentaPanel.jLabel3.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(DodajEdytujStudentaPanel.class, "DodajEdytujStudentaPanel.jLabel4.text")); // NOI18N

        imieField.setText(org.openide.util.NbBundle.getMessage(DodajEdytujStudentaPanel.class, "DodajEdytujStudentaPanel.imieField.text")); // NOI18N
        imieField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                imieFieldActionPerformed(evt);
            }
        });

        nazwiskoField.setText(org.openide.util.NbBundle.getMessage(DodajEdytujStudentaPanel.class, "DodajEdytujStudentaPanel.nazwiskoField.text")); // NOI18N
        nazwiskoField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nazwiskoFieldActionPerformed(evt);
            }
        });

        emailField.setText(org.openide.util.NbBundle.getMessage(DodajEdytujStudentaPanel.class, "DodajEdytujStudentaPanel.emailField.text")); // NOI18N

        indeksField.setText(org.openide.util.NbBundle.getMessage(DodajEdytujStudentaPanel.class, "DodajEdytujStudentaPanel.indeksField.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(emailField, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(indeksField, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nazwiskoField, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(imieField, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(imieField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(nazwiskoField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(emailField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(indeksField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    public boolean addStudent() {
        String imie = imieField.getText();
        String nazwisko = nazwiskoField.getText();
        String email = emailField.getText();
        if (!isValidEmailAddress(email)) {
            JOptionPane.showMessageDialog(this, "Niepoprawny adres email!", "Niepoprawne dane - BŁĄD", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        try {
            int indeksNr = Integer.parseInt(indeksField.getText());
            Student.add(imie, nazwisko, email, indeksNr);
            NotificationDisplayer.getDefault().notify("Dodano studenta", new ImageIcon(), "", null, NotificationDisplayer.Priority.LOW, NotificationDisplayer.Category.INFO);
            openStudentsList();
            return true;
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "Niepoprawny numer indeksu!", "Niepoprawne dane - BŁĄD", JOptionPane.ERROR_MESSAGE);
            return false;
        }
    }

    public boolean editStudent(int id) {
        String imie = imieField.getText();
        String nazwisko = nazwiskoField.getText();
        String email = emailField.getText();
        if (!isValidEmailAddress(email)) {
            JOptionPane.showMessageDialog(this, "Niepoprawny adres email!", "Niepoprawne dane - BŁĄD", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        try {
            int indeksNr = Integer.parseInt(indeksField.getText());
            Student student = new Student(id, imie, nazwisko, email, indeksNr);
            Student.edit(student);
            NotificationDisplayer.getDefault().notify("Zmieniono dane studenta", new ImageIcon(), "", null, NotificationDisplayer.Priority.LOW, NotificationDisplayer.Category.INFO);
            openStudentsList();
            return true;
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "Niepoprawny numer indeksu!", "Niepoprawne dane - BŁĄD", JOptionPane.ERROR_MESSAGE);
            return false;
        }
    }

    public boolean isValidEmailAddress(String email) {
        java.util.regex.Pattern p = java.util.regex.Pattern.compile(".+@.+\\.[a-z]+");
        java.util.regex.Matcher m = p.matcher(email);
        return m.matches();
    }

    private void openStudentsList() {
        StudentsListTopComponent window = (StudentsListTopComponent) WindowManager.getDefault().findTopComponent("StudentsListTopComponent");
        window.componentOpened();
        window.open();
        window.requestActive();
    }

    private void imieFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_imieFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_imieFieldActionPerformed

    private void nazwiskoFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nazwiskoFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_nazwiskoFieldActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField emailField;
    private javax.swing.JTextField imieField;
    private javax.swing.JTextField indeksField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField nazwiskoField;
    // End of variables declaration//GEN-END:variables
}
